<?php

namespace app\index\controller;

use app\admin\model\Advertisement;
use app\admin\model\Column;
use think\Controller;
use think\Db;
use think\Request;

class CommonController extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    protected  $global = [
        'nav'=>'',
         'banner'=>'',
         'ad'=>'',
         'footer'=>'',
         'friend_link'=>''
    ];
    protected  $title;
    protected  $keyword;
    protected  $domain;
    protected  $copyright;
    protected  $description;
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->nav();
        $this->friend_link();
        $this->website();
        $this->assign('global',$this->global);

        $this->assign('title',$this->title);
        $this->assign('keyword',$this->keyword);
        $this->assign('domain',$this->domain);
        $this->assign('copyright',$this->copyright);
        $this->assign('description',$this->description);

    }
    public function friend_link(){
        $friend_link = Db::name('friend_link')->select();
        $this->global['friend_link'] = $friend_link ;
    }
    public function website(){
        $setting = Db::name('setting')->select();
        $build =[];
        foreach ($setting as $item) {
            $build[$item['name']] =$item['value'];
        }
        $this->title =$build['web_title'];
        $this->domain =$build['web_domian'];
        $this->keyword = $build['keywords'];
        $this->description = $build['descript'];
        $this->copyright = $build['copyright'];
    }
    public function nav(){
     $column =    (new Column())->order('sort','asc')->select();
     $build = [];
        foreach ($column as $item) {
            $build[$item['column_id']] = $item;
     }
      $pid = 0;
         $nav =  $this->build_nav($build,$pid);
        $this->global['nav'] = $nav;

    }
    private function build_nav($data,$pid)
    {
        $html='';
        foreach($data as $row)
        {
            if($row['column_parent']==$pid)
            {
                $class = 'class="dropdown"' ;
                $url = '/index/'.$row['control'].'/'.$row['method'];
                $html.="<li ".$class." >";

                $html.=' <a href="'.$url.'">';
                $html.= $row['column_name'];
                $html.='</a>';
                $html.=$this->build_nav($data,$row['column_id']);
                $html.="</li>";
            }
        }
        if ($pid){
            return $html?'<ul class="dropdown-menu">'.$html.'</ul>':$html;
        }else{
            return $html?'<ul class="navbar-nav">'.$html.'<li>
                            <span class="theme_btn" style="border-radius: 30px">
                            <a href="login" style="color:#b76dfe">登录</a>
                            |
                            <a href="register" style="color:#b76dfe">注册</a>
                              </span>
                        </li></ul>':$html;
        }
    }

}
